package com.ymatou.apigateway.testcase;

import java.util.Arrays;
import java.util.List;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.apigateway.parameter.AesUtil;
import com.ymatou.apigateway.parameter.OpenapiReq;
import com.ymatou.apigateway.parameter.SqProductPicUpdateBean;
import com.ymatou.apigateway.service.OpenapiService;
import com.ymatou.apigateway.service.SqProductPicUpdateCall;
import com.shop2cn.iapi.agentproducts.parameter.AddAgentProductBean;
import com.ymt.core.tool.FormatUtil;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.*;
import com.ymt.utils.tag.*;
import com.ymttest.business.service.AddAgentProductCallService;
import com.ymttest.database.annotation.SqlMapperFactory;
import com.ymttest.database.annotation.product.IntergratedProductMapper;
import com.ymttest.database.model.Application;
import com.ymttest.database.model.AuthCode;
import com.ymttest.database.model.YmtProductsI;
import com.ymttest.database.sqlwapper.OpenApiWapper;
import com.ymttest.database.sqlwapper.YmtCatalogsIWapper;
import com.ymttest.database.sqlwapper.YmtProductsIWapper;
import com.ymttest.database.sqlwapper.godmch.mch_infoWapper;
import com.ymttest.utils.EnvSetup;
import com.ymttest.utils.TestDataManager;
import com.ymttest.utils.YMTDateUtil;
/**
*更新商品图片
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_SqProductPicUpdate{
	private static OpenapiReq openapiReq;
	private static SqProductPicUpdateBean sqproductpicupdateBean;
	private static SqProductPicUpdateCall sqproductpicupdateCall;

	private static YmtProductsIWapper ymtProductsIWapper = new YmtProductsIWapper();


	// vip渠道
	private static int RootUserId = Integer.valueOf(EnvSetup
			.getData("autotest_goodsOwnerId"));
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("更新商品图片");
	}

	@Before
	public void caseUp() {
		sqproductpicupdateBean=new SqProductPicUpdateBean();
		sqproductpicupdateCall=new SqProductPicUpdateCall();
		openapiReq = new OpenapiReq();
		openapiReq.setMethod("sq.product.pic.update");
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(sqproductpicupdateCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_SqProductPicUpdate_001() {
		Logger.start(true,"更新商品图片");
		try {
			//前置数据
			AddAgentProductBean addAgentProductBean = AddAgentProductCallService.initAddAgentProduct(RootUserId,
                    "testname", false);
            String productId = AddAgentProductCallService.AddAgentProduct(addAgentProductBean);
			
			sqproductpicupdateBean.setMain_pic_list(Arrays.asList("http://pic1.ymatou.com/G02/shangou/M07/00/93/CgvUBVfCiVSAeWRoAAJnPjwA0tM667_n_w_o.jpg"));
			sqproductpicupdateBean.setProduct_id(productId);
			
			
			OpenApiWapper openApiWapper = new OpenApiWapper();
			Application application = openApiWapper
					.selectApplicationBySellerId((long) RootUserId).get(0);
			String app_id = application.getApp_id();
			String app_secret = application.getApp_secret();
			AuthCode authCode = openApiWapper.selectAuthCodeByAppId(app_id)
					.get(0);
			openapiReq.setApp_id(app_id);
			openapiReq.setSign_method("MD5");
			openapiReq.setAuth_code(authCode.getAuth_code());
			openapiReq.setTimestamp(YMTDateUtil.getSysTime());
			openapiReq.setNonce_str(TestDataManager.getRandomNum(31));
			openapiReq.setBiz_content(new FormatUtil()
					.beanToGSONString(sqproductpicupdateBean));
			openapiReq.setSign(OpenapiService.generateMD5(openapiReq,
					AesUtil.decrypt(app_secret)));
			String url = String
					.format("http://apigateway.ymatou.com/apigateway/v1?app_id=%s&method=sq.product.pic.update",
							app_id);
			sqproductpicupdateCall.setUrl(url);
			//openapi设置完成
			
			sqproductpicupdateCall.setData(openapiReq);
			sqproductpicupdateCall.callService();			
			Logger.verifyEquals("0000", sqproductpicupdateCall.getString("code"), "验证返回");
			List<YmtProductsI> product=ymtProductsIWapper.selectProductByProductId(productId);
			Logger.verifyEquals(sqproductpicupdateBean.getMain_pic_list().get(0),product.get(0).getsPicUrl() , "验证picUrl");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}